import {
	GET_MESSAGE_COUNT,
	GET_MESSAGE_INFO,
	GET_MESSAGE_TREANDS,
	DELETE_MESSAGE,
} from './actions'

const prefixUrl = () => uni.$http.messagePrefixUrl;
export default {
	state:{
		loadingCount: true,
		loadingSystem: true,
		loadingTreands: true,
		countList: {
			systemMessage: {},
			interactMessage: {},
		},
		systemList: [],
		interList: [],
	},
	getters: {

	},
	mutations: {
		getCount(state, { data, loading }) {
			state.countList.systemMessage = data && data.systemMessage ? data.systemMessage : {}
			state.countList.interactMessage = data && data.interactMessage ? data.interactMessage : {}
			state.loadingCount = loading;
		},
		gerSystemList(state, { data, loading }) {
			state.systemList = data
			state.loadingSystem = loading;
		},
		gerIntserList(state, { data, loading }) {
			state.interList = data;
			state.loadingTreands = loading;
		},
	},
	actions: {
		async [GET_MESSAGE_COUNT]({ commit }) {
			try{
				const res = await uni.$http.post(prefixUrl() + '/message/getMessageCount')
				if (res && res.data) {
					const {
						systemMessage = [],
						interactMessage = [],
					} = res.data;

					let total = 0;
					total = systemMessage && systemMessage.count ? systemMessage.count - 0 + total : total;
					total = interactMessage && interactMessage.count ? interactMessage.count - 0 + total : total;

					if (total === 0) {
						uni.removeTabBarBadge({
							index: 2,
						})
					} else {
						uni.setTabBarBadge({
							index: 2,  //tabBar序号，从0开始计数
							text: `${total || ''}`
						})
					}
					commit('getCount', { data: { systemMessage, interactMessage }, loading: false })
				} else {
					uni.removeTabBarBadge({
						index: 2,
					})
					commit('getCount', { data: {}, loading: false })
				}
				return res;
			} catch(err) {
				commit('getCount', { data: {}, loading: false })
				console.log(err)
				return err;
			}
		},
		async [GET_MESSAGE_INFO]({ commit }, params) {
			try{
				const res = await uni.$http.post(prefixUrl() + '/message/getMessageInformation', params)
				if (res && res.data) {
					commit('gerSystemList', { data: res.data, loading: false })
					return res.data.length ? res.data.map(item => ({
            ...item,
            show: false,
          })) : [];
				} else {
					commit('gerSystemList', { data: [], loading: false })
					return [];
				}
			} catch(err) {
				commit('gerSystemList', { data: [], loading: false })
				return [];
			}
		},
		async [GET_MESSAGE_TREANDS]({ commit }, params) {
			try{
				const res = await uni.$http.post(prefixUrl() + '/message/getMessageTreands', params)
				if (res && res.data) {
					commit('gerIntserList', { data: res.data, loading: false })
					return res.data.length ? res.data.map(item => ({
            ...item,
            show: false,
          })) : [];
				} else {
					commit('gerIntserList', { data: [], loading: false })
					return [];
				}
			} catch(err) {
				commit('gerIntserList', { data: [], loading: false })
				return [];
			}
		},
		async [DELETE_MESSAGE]({ commit }, params) {
			try{
				const res = await uni.$http.post(prefixUrl() + '/message/del', params)
				if (res && res.data) {
					uni.showToast({
						title: '删除成功',
						icon: 'none',
					});
					return res.data
				} else {
					return '';
				}
			} catch(err) {
				return '';
			}
		}
	}
}
